package com.xuanchen.system.sysdept.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuanchen.system.sysdept.entity.SysDept;
import com.xuanchen.system.sysdept.entity.SysDeptTreeVO;
import com.xuanchen.system.sysdept.service.ISysDeptService;
import io.lettuce.core.json.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 控制器 --> 部门
 *
 * @author XuanChen
 * @date 2025-11-12
 */
@RestController
@RequestMapping("/system/dept")
public class SysDeptController {
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 获取部门树结构数据
     *
     * @return 部门树节点列表
     */
    @GetMapping("/getDeptTree")
    public List<SysDeptTreeVO> getDeptTree() {
        return sysDeptService.getDeptTree();
    }

    /**
     * 获取子部门树结构数据
     *
     * @param parentDeptCode 父部门编码
     * @return 子部门树节点列表
     */
    @GetMapping("/getChildDeptTree")
    public List<SysDeptTreeVO> getChildDeptTree(String parentDeptCode) {
        return sysDeptService.getChildDeptTree(parentDeptCode);
    }

    /**
     * 获取选中的部门信息
     *
     * @param deptCode
     * @return
     */
    @GetMapping("/getSelectedDept")
    public SysDept getSelectedDept(String deptCode) {
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept_code", deptCode);
        return sysDeptService.getOne(queryWrapper);
    }

    /**
     * 创建部门编码
     *
     * @param jsonObject
     * @return
     */
    @PostMapping("/createDeptCode")
    public String createDeptCode(@RequestBody JsonObject jsonObject) {
        String parentDeptCode = jsonObject.get("parentDeptCode").toString();
        return sysDeptService.createDeptCode(parentDeptCode);
    }

}
